package com.geeklan.smart.ui.viewmodel.common;

import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.geeklan.smart.data.model.ApiResponse;
import com.geeklan.smart.data.model.AppProblem;
import com.geeklan.smart.data.model.BannerResponse;
import com.geeklan.smart.data.model.Message;
import com.geeklan.smart.data.model.RequestReadMsg;
import com.geeklan.smart.data.model.VersionResponse;
import com.geeklan.smart.data.repository.RepositoryCallback;
import com.geeklan.smart.data.repository.msg.CommonRepository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CommonViewModel extends ViewModel {
    private final CommonRepository msgRepository;
    public CommonViewModel(CommonRepository msgRepository) {
        this.msgRepository = msgRepository;
    }

    private MutableLiveData<ApiResponse<List<Message>>> msgListLiveData = new MutableLiveData<>();
    private MutableLiveData<ApiResponse<List<VersionResponse>>> versionListLiveData = new MutableLiveData<>();
    private MutableLiveData<ApiResponse<List<AppProblem>>> problemListLiveData = new MutableLiveData<>();
    private MutableLiveData<BannerResponse> bannerListLiveData = new MutableLiveData<>();

    public MutableLiveData<BannerResponse> getBannerListLiveData() {
        return bannerListLiveData;
    }

    public MutableLiveData<ApiResponse<List<AppProblem>>> getProblemListLiveData() {
        return problemListLiveData;
    }

    public MutableLiveData<ApiResponse<List<VersionResponse>>> getVersionListLiveData() {
        return versionListLiveData;
    }

    public MutableLiveData<ApiResponse<List<Message>>> getMsgListLiveData() {
        return msgListLiveData;
    }

    private MutableLiveData<ApiResponse> readMsgListLiveData = new MutableLiveData<>();
    public MutableLiveData<ApiResponse> getReadMsgListLiveData() {
        return readMsgListLiveData;
    }

    private MutableLiveData<String> msgErrorLiveData = new MutableLiveData<>();
    private MutableLiveData<String> versionErrorLiveData = new MutableLiveData<>();
    private MutableLiveData<String> problemErrorLiveData = new MutableLiveData<>();
    private MutableLiveData<String> bannerErrorLiveData = new MutableLiveData<>();

    public MutableLiveData<String> getBannerErrorLiveData() {
        return bannerErrorLiveData;
    }

    public MutableLiveData<String> getProblemErrorLiveData() {
        return problemErrorLiveData;
    }

    public MutableLiveData<String> getVersionErrorLiveData() {
        return versionErrorLiveData;
    }

    public MutableLiveData<String> getMsgErrorLiveData() {
        return msgErrorLiveData;
    }


    private MutableLiveData<String> readMsgErrorLiveData = new MutableLiveData<>();

    public MutableLiveData<String> getReadMsgErrorLiveData() {
        return readMsgErrorLiveData;
    }

    public void getMsgList(int limit,int page,int type,int readFlag) {
        Map<String, Integer> options = new HashMap<>();
        options.put("limit",limit);
        options.put("page",page);
        options.put("type",type);
        options.put("readFlag",readFlag);
        msgRepository.getMsgList(options,new RepositoryCallback<ApiResponse<List<Message>>>() {
            @Override
            public void onSuccess(ApiResponse<List<Message>> data) {
                if (data != null) {
                    msgListLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                msgErrorLiveData.setValue(e.getMessage());
            }
        });
    }
    public void getAppVersion(int limit,int page) {
        Map<String, Integer> options = new HashMap<>();
        options.put("limit",limit);
        options.put("page",page);
        msgRepository.getAppVersion(options,new RepositoryCallback<ApiResponse<List<VersionResponse>>>() {
            @Override
            public void onSuccess(ApiResponse<List<VersionResponse>> data) {
                if (data != null) {
                    versionListLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                versionErrorLiveData.setValue(e.getMessage());
            }
        });
    }
    public void getAppProblem(int limit,int page) {
        Map<String, Integer> options = new HashMap<>();
        options.put("limit",limit);
        options.put("page",page);
        msgRepository.getAppProblem(options,new RepositoryCallback<ApiResponse<List<AppProblem>>>() {
            @Override
            public void onSuccess(ApiResponse<List<AppProblem>> data) {
                if (data != null) {
                    problemListLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                problemErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void getAppBanner() {
        msgRepository.getAppBanner(new RepositoryCallback<ApiResponse<BannerResponse>>() {
            @Override
            public void onSuccess(ApiResponse<BannerResponse> data) {
                if (data != null) {
                    bannerListLiveData.setValue(data.getData());
                }
            }

            @Override
            public void onError(Exception e) {
//                bannerErrorLiveData.setValue(e.getMessage());
            }
        });
    }

    public void readMsg(int id,int type) {
        RequestReadMsg requestReadMsg = new RequestReadMsg();
        requestReadMsg.setId(id);
        requestReadMsg.setType(type);
        msgRepository.readMsg(requestReadMsg,new RepositoryCallback<ApiResponse>() {
            @Override
            public void onSuccess(ApiResponse data) {
                if (data != null) {
                    readMsgListLiveData.setValue(data);
                }
            }

            @Override
            public void onError(Exception e) {
                readMsgErrorLiveData.setValue(e.getMessage());
            }
        });
    }
}
